package logic

import (
	"automated-modeling/automatedrpc/automatedrpc"
	"automated-modeling/automatedrpc/internal/svc"
	"automated-modeling/models/mysql"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductsAddLogic {
	return &ProductsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductsAddLogic) ProductsAdd(in *automatedrpc.ProductsAddRequest) (*automatedrpc.ProductsAddResponse, error) {
	// todo: add your logic here and delete this line
	var products mysql.Products
	products = mysql.Products{
		Name:   in.Name,
		Cate:   in.Cate,
		Cost:   float64(in.Cost),
		Market: float64(in.Market),
		Image:  in.Image,
		UserId: int(in.UseId),
	}

	err := mysql.DB.Debug().Create(&products).Error
	if err != nil {
		return nil, fmt.Errorf("新增失败")
	}

	return &automatedrpc.ProductsAddResponse{Id: int64(products.ID)}, nil
}
